<template>
  <!---搜索表单-->
    <div class="search-div">
        <el-form label-width="70px" size="small">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="关键字">
                        <el-input v-model="queryDto.name" style="width: 100%" placeholder="教师姓名"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="所属学院">
                        <el-select v-model="queryDto.collegeId" style="width: 100%" placeholder="请选择学院" filterable>
                            <el-option v-for="item in collegeList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10">
                    <el-form-item label="入职日期">
                        <el-date-picker v-model="hireDates" type="daterange" range-separator="To"
                                        start-placeholder="开始时间"
                                        end-placeholder="结束时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row style="display:flex">
                <el-button type="primary" size="small" @click="searchTeacher"> 搜索</el-button>
                <el-button size="small" @click="resetData">重置</el-button>
            </el-row>
        </el-form>
    </div>

  <!--添加按钮-->
    <div class="tools-div">
        <el-button type="success" size="small" @click="addShow">添 加</el-button>
    </div>

    <el-dialog v-model="dialogVisible" title="添加或修改" width="40%" @close="handleDialogClose">
        <el-form label-width="120px">
            <el-form-item label="教师工号">
                <el-input v-model="teacher.teacherNumber" :disabled="teacher.teacherNumber !== ''"/>
            </el-form-item>
            <el-form-item label="教师姓名">
                <el-input v-model="teacher.name"/>
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="teacher.sex">
                    <el-radio :label="1">男</el-radio>
                    <el-radio :label="0">女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="职称">
                <el-input v-model="teacher.title"/>
            </el-form-item>
            <el-form-item label="联系方式">
                <el-input v-model="teacher.contactInformation"/>
            </el-form-item>
            <el-form-item label="学历">
                <el-input v-model="teacher.education"/>
            </el-form-item>
            <el-form-item label="入职日期">
                <el-date-picker v-model="teacher.hireDate" arrow-control placeholder="请选择日期"/>
            </el-form-item>
            <el-form-item label="办公地址">
                <el-input v-model="teacher.officeAddress"/>
            </el-form-item>
            <el-form-item label="所属学院">
                <el-select v-model="teacher.collegeId" style="width: 100%" placeholder="请选择学院" filterable>
                    <el-option v-for="item in collegeList" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

  <!---数据表格-->
    <el-table :data="list" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="60px"/>
        <el-table-column prop="teacherNumber" label="工号" width="130px"/>
        <el-table-column prop="name" label="教师姓名" width="100px"/>
        <el-table-column prop="sex" label="性别" #default="scope" align="center" width="70px">
            {{ scope.row.sex == 1 ? '男' : '女' }}
        </el-table-column>
        <el-table-column prop="title" label="职称" width="100px"/>
        <el-table-column prop="contactInformation" label="联系方式" width="140px"/>
        <el-table-column prop="education" label="学历" width="80px"/>
        <el-table-column prop="hireDate" label="入职日期" width="100px"/>
        <el-table-column prop="officeAddress" label="办公地址" width="140px"/>
        <el-table-column prop="collegeName" label="所属学院" width="250px"/>
        <el-table-column prop="createTime" label="创建时间" width="160px" align="center"/>
        <el-table-column prop="updateTime" label="更新时间" width="160px" align="center"/>
        <el-table-column label="操作" align="center" #default="scope" width="140px" fixed="right">
            <el-button type="primary" size="small" @click="editTeacher(scope.row)"> 修改</el-button>
            <el-button type="danger" size="small" @click="deleteById(scope.row)"> 删除</el-button>
        </el-table-column>
    </el-table>
    <el-pagination v-model:current-page="pageParams.page" v-model:page-size="pageParams.limit"
                   :page-sizes="[5, 10, 20, 50, 100]" @size-change="fetchData" @current-change="fetchData"
                   layout="total, sizes, prev, pager, next" :total="total"/>
</template>

<!-- script部分修改内容 -->
<script setup>
import {onMounted, ref} from 'vue'
import {DeleteTeacherByTeacherNumber, GetTeacherListByPage, SaveTeacher, UpdateTeacher} from '@/api/teacher'
import {ElMessage, ElMessageBox} from 'element-plus'
import {GetAllCollege} from '@/api/college'

// 删除数据
const deleteById = row => {
    ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const {code} = await DeleteTeacherByTeacherNumber(row.teacherNumber)
        if (code === 200) {
            ElMessage.success('删除成功')
            pageParams.value.page = 1
            await fetchData()
        }
    })
}

// 修改按钮点击事件处理函数
const editTeacher = row => {
    teacher.value = row
    dialogVisible.value = true
}

//表单数据模型
const defaultForm = {
    id: '',
    teacherNumber: '',
    name: '',
    sex: "",
    title: '',
    contactInformation: '',
    education: '',
    hireDate: '',
    officeAddress: '',
    collegeId: ''
}

const teacher = ref(defaultForm) // 使用ref包裹该对象，使用reactive不方便进行重置
const collegeList = ref([])

const getCollegeList = async () => {
    const {data} = await GetAllCollege()
    collegeList.value = data
}

const handleDialogClose = () => {
    teacher.value = {...defaultForm}; // 重置数据为默认值
}

// 添加角色
const submit = async () => {
    if (!teacher.value.id) {
        const {code} = await SaveTeacher(teacher.value)
        if (code === 200) {
            dialogVisible.value = false
            ElMessage.success('操作成功')
            await fetchData()
        }
    } else {
        const {code} = await UpdateTeacher(teacher.value)
        if (code === 200) {
            dialogVisible.value = false
            ElMessage.success('操作成功')
            await fetchData()
        }
    }
}

// 分页条总记录数
let total = ref(0)

// 定义表格数据模型
let list = ref([])

// 控制对话是否展示的变量
const dialogVisible = ref(false)

//进入添加
const addShow = () => {
    dialogVisible.value = true
}

//分页数据
const pageParamsForm = {
    page: 1, // 页码
    limit: 10, // 每页记录数
}
const pageParams = ref(pageParamsForm) // 将pageParamsForm包装成支持响应式的对象

// 搜索表单数据
const queryDto = ref({
    name: '',
    collegeId: '',
    createTimeBegin: '',
    createTimeEnd: '',
})

const hireDates = ref([])
// 页面加载完毕以后请求后端接口获取数据
onMounted(() => {
    fetchData()
    getCollegeList()
})

// 搜索按钮点击事件处理函数
const searchTeacher = () => {
    fetchData()
}
// 重置按钮点击事件处理函数
const resetData = () => {
    queryDto.value = {}
    hireDates.value = []
}

// 远程调用后端分页查询接口
const fetchData = async () => {
    if (hireDates.value.length == 2) {
        queryDto.value.createTimeBegin = hireDates.value[0]
        queryDto.value.createTimeEnd = hireDates.value[1]
    }
    const {data} = await GetTeacherListByPage(pageParams.value.page, pageParams.value.limit, queryDto.value)
    list.value = data.list
    total.value = data.total
}
</script>

<style scoped>
.search-div {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}

.tools-div {
    margin: 10px 0;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}
</style>
